// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:

const { show } = require("../game/alert");
const { shopData } = require("./gameData");

//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
    extends: cc.Component,

    properties: {
        foodName:cc.Label,
        stock:cc.Label,
        sales:cc.Label,
        transport:cc.Label,
        price:cc.Label,
        buyNum: cc.EditBox,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        
    },
    buyCommon(target, i) {
        if(!this.buyNum.string || parseInt(this.buyNum.string) <= 0){
            this.buyNum.string = 100;
            // show('未来批发','弄啥呢，搞多少货');
            // return;
        }
        let cost = target.target.parent.cost;
        let buyCost = cost * parseInt(this.buyNum.string) * i * 0.2;
        if(buyCost <= shop.money) {
            shop.money -= buyCost;
            let id = target.target.parent.id;
            let tmpFood = shop.foodList.find(val => {return val.id == id});
            // food.stock = parseInt(this.buyNum.string);
            let food = {
                transport: tmpFood.level * 60/i,
                stock: parseInt(this.buyNum.string),
                id: tmpFood.id,
                name: tmpFood.name
            }
            // food.transport = food.level * 60/i;
            shop.transportList.push(food);
            show('未来批发','货车司机已出发，请耐心等待配送');
            //添加到进货队列
        }else {
            show('未来金融','公司账户余额不足');
        }
    },
    
    // update (dt) {},
});
